<?php
defined('IN_XLP') or exit('Access Denied');

$Document = array(
	'pageid'   => 'gamearea-index', //页面标示
	'pagename' => '区服管理', //当前页面名称
	'mycss'	   => array(), //加载的css样式表
	'myjs'	   => array(), //加载的js脚本
	//'footerjs'=>array('content/highcharts', 'content/exporting',),
	'footerjs' => array(),
	'head'	   => true, //加载头部文件
);

include getTpl('header', 'public');
?>
<!--顶部导航 开始-->
<?php include getTpl('top', 'public');?>
<!--顶部导航 结束-->
<!--左侧菜单 开始-->
<?php include getTpl('nav', 'public');?>
<!--左侧菜单 结束-->
<!--主体 开始-->
<!-- content -->
<section id="content">
	<section class="main padder">
		<div class="clearfix">
			<h4><i class="fa fa-table"></i>区服管理</h4>
		</div>
		<div class="row">
			<div class="col-lg-12">
				<section class="panel">

					<header class="panel-heading">
						区服信息
					</header>

					<div class="panel-body">
						<div class="row text-small">
							<div class="col-sm-1 m-b-mini">
								<a href="<?php echo U('gamearea/add'); ?>" class="btn btn-sm btn-white">添加新区服</a>
							</div>
							<div class="col-sm-4">
		                        <div class="col-sm-6 text-left">
		                           	<select class="form-control" name="game_id" id="gameId">
		                           		<option value="0">全部游戏</option>
		                           		<?php foreach ($games as $k => $v): ?>
		                           			<option value="<?php echo $v['id'] ?>" <?php if($gameid == $v['id']) { echo 'selected'; } ?>><?php echo $v['name'] ?></option>
		                           		<?php endforeach ?>
		                           	</select>
		                   		</div>
		                    </div>
						</div>
					</div>

					<style>.table th,.table td,.table input{text-align:center;}</style>
					<div class="table-responsive">
						<table class="table table-striped b-t text-small">
							<thead>
								<tr>
									<th>ID</th>
									<th>所属游戏</th>
									<th>游戏区</th>
									<th>游戏服</th>
									<th>添加/编辑服名称</th>
									<th>添加/编辑服编码</th>
									<th>添加/编辑服跨区</th>
									<th>区排序</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
								<?php if (count($game_area)): ?>
								<?php foreach ($game_area as $v): ?>
								<tr>
									<td style="line-height:33px;">
										<?php echo $v['id']; ?>
										<input type="hidden" name="area_id" value="<?php echo $v['id'] ?>">
									</td>
									<td style="line-height:33px;color: #428bca">
										<?php echo $v['game']; ?>
									</td>
									<td style="line-height:33px;">
										<?php echo $v['area_name']; ?>
										<input type="hidden" name="area_name" value="<?php echo $v['area_name'] ?>">
									</td>
									<td>
										<select name="server_id" class="spec_value form-control">
										<option value="0">选择需要编辑的服 OR 直接添加</option>
										<?php foreach ($v['server'] as $value): ?>
										<option value="<?php echo $value['id']; ?>" data-value="<?php echo $value['server_name']; ?>" data-tips="<?php echo $value['abridge']; ?>" data-cross="<?php echo $value['cross_no'] ?>">
											<?php echo $value['server_name'].' ['.$value['abridge'].']'; ?>
										</option>
										<?php endforeach ?>
										</select>
								    </td>
									<td class="edit_server_name">
										<input type="text" name="server_name" placeholder="添加服名称" class="form-control">
									</td>
									<td class="edit_server_abridge">
										<input type="text" name="abridge" placeholder="添加服编码" class="form-control">
									</td>
									<td class="edit_cross">
										<input type="text" name="cross" placeholder="添加跨区编码（可选）" class="form-control">
									</td>
									<td>
										<input type="text" name="sort" placeholder="区排序" class="form-control" value="<?php echo $v['sort'] ?>">
									</td>
									<td>
										<a class="save_spec" style="line-height:33px;" href="javascript:;">保存</a>
										<a class="red area_sort" style="line-height:33px;" href="javascript:;">排序</a>
									</td>
								</tr>
								<?php endforeach ?>
								<?php else: ?>
								<tr><td colspan="8" align="center">暂无数据</td></tr>
								<?php endif ?>
							</tbody>
						</table>
					</div>
					<footer class="panel-footer"> 
			            <div class="row"> 
			              <div class="col-sm-12 text-right text-center-sm"> 
			                <?php echo page($total,$page,'', 20, 'page');?>  
			              </div> 
			            </div> 
		          	</footer> 
				</section>
			</div>
		</div>
	</section>
</section>
<!--/ content --> 
<!--主体 结束-->
<script>
$(function(){
	$('.spec_value').change(function(){
		var index = $(this).val();
		var edit_server_name = $(this).parent().siblings('.edit_server_name').children('input');
		var edit_server_abridge = $(this).parent().siblings('.edit_server_abridge').children('input');
		var edit_cross = $(this).parent().siblings('.edit_cross').children('input');

		if(index != 0) {
			edit_server_name.val($(this).children('option:selected').data('value'));
			edit_server_abridge.val($(this).children('option:selected').data('tips'));
			edit_cross.val($(this).children('option:selected').data('cross'));
		} else {
			edit_server_name.val('');
			edit_server_abridge.val('');
			edit_cross.val('');
		}
	});

	$('.save_spec').click(function(){
		var $parent = $(this).parent().parent();
		// console.log($parent.get(0));
		var edit_server_name = $(this).parent().siblings('.edit_server_name').children('input').val();
		if($.trim(edit_server_name) == '') {
			Msg.error('游戏服不能为空');
			return false;
		}

    	Msg.loading();
		$.post('<?php echo U("gamearea/server_value_add") ?>', {
			"area_id"       : $parent.find('input[name="area_id"]').val(),
			"area_name"     : $parent.find('input[name="area_name"]').val(),
			"server_id" 	: $parent.find('select option:selected').val(),
			"server_name"   : $parent.find('input[name="server_name"]').val(),
			"abridge"     	: $parent.find('input[name="abridge"]').val(),
			"cross_no"     	: $parent.find('input[name="cross"]').val()
		}, function(json){
			Msg.hide();
			if(json.err==1) {
				Msg.error(json.msg);
			} else {
				Msg.ok(json.msg);
			}
		}, 'JSON');
		
	});

	//更新排序
	$('.area_sort').click(function(){
		var $parent = $(this).parent().parent();
		Msg.loading();
		$.post('<?php echo U("gamearea/update_area_sort") ?>', {"area_id" : $parent.find('input[name="area_id"]').val(), "sort" : $parent.find('input[name="sort"]').val()}, function(result){
				Msg.hide();
				if(result.err==1) {
					Msg.error(result.msg);
				} else {
					Msg.ok(result.msg);
				}
		},"json");
	});

	//游戏筛选
	$('#gameId').change(function(){
		var game_id = $(this).val();
		var url = "<?php echo U('gamearea/index') ?>"+'?game_id='+game_id;
		window.location.href = url;
	});

});
</script>
<?php include getTpl('footer', 'public'); ?>